package com.lvy.hczn.front.business.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lvy.hczn.front.system.domain.business.Shelves;
import com.lvy.hczn.front.business.service.ShelvesService;
import com.lvy.hczn.front.business.util.ResultUtil;
import com.lvy.hczn.front.common.exception.UtilException;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("/api/dt/wcs/shelves/")
@RestController
@Slf4j
public class ShelvesController {

    @Autowired
    private ShelvesService shelvesService;

    @ApiOperation(value = "分拣加工任务，按原料分拣", notes = "分拣加工任务，按原料分拣")
    @PostMapping("initShelves")
    public ResultUtil initShelves(@RequestBody Map<String, Object> map) {
        try {
            if (map == null || map.isEmpty()) {
                return ResultUtil.failed();
            }
            //库位数
            Integer xAxis = (Integer) map.get("xAxis");
            //层数
            Integer yAxis = (Integer) map.get("yAxis");
            //多少行
            Integer row = (Integer) map.get("row");
            String[] yArray = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
            shelvesService.remove(Wrappers.<Shelves>lambdaQuery().isNotNull(Shelves::getId));
            List<Shelves> list = new ArrayList<>();
            /*for (int i = 0; i < row; i++) {
                Integer rowNum = i + 1;
                for (int j = 0; j < yAxis; j++) {
                    Integer jNum = j + 1;
                    for (int k = 0; k < xAxis; k++) {
                        Shelves shelves = new Shelves();
                        //行-列-层
                        shelves.setShelvesCode(rowNum + "-" + (k + 1) + "-" + jNum);
                        shelves.setRowNum(String.valueOf(rowNum));
                        shelves.setVolume(new BigDecimal(10000));
                        shelves.setOccupied(0);
                        list.add(shelves);
                    }
                }
            }*/
            Integer total = row * yAxis * xAxis;
            for (int i = 0; i < total; i++) {
                Shelves shelves = new Shelves();
                //行-列-层
                shelves.setShelvesCode(String.valueOf(i + 1));
                shelves.setRowNum(String.valueOf(row));
                shelves.setVolume(new BigDecimal(10000));
                shelves.setOccupied(0);
                list.add(shelves);
            }
            shelvesService.saveBatch(list);
            return ResultUtil.ok();
        } catch (Exception e) {
            log.error("initShelves:{}", e);
            if (e instanceof UtilException) {
                return ResultUtil.failed(e.getMessage());
            } else {
                return ResultUtil.failed("系统异常");
            }
        }
    }
}
